﻿namespace AC.ExtendedRenderer.Toolkit
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(PropertyGrid))]
    public class KryptonPropertyGrid : PropertyGrid
    {
        private Color _gradientMiddleColor = Color.Gray;
        private IPalette _palette;
        private PaletteRedirect _paletteRedirect;

        public KryptonPropertyGrid()
        {
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect = new PaletteRedirect(this._palette);
            this.InitColors();
        }

        private void InitColors()
        {
            base.ToolStripRenderer = ToolStripManager.Renderer;
            this._gradientMiddleColor = this._palette.ColorTable.ToolStripGradientMiddle;
            base.HelpBackColor = this._palette.ColorTable.MenuStripGradientBegin;
            base.HelpForeColor = this._palette.ColorTable.StatusStripText;
            base.LineColor = this._palette.ColorTable.ToolStripGradientMiddle;
            base.CategoryForeColor = this._palette.ColorTable.StatusStripText;
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (this._palette != null)
            {
                this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect.Target = this._palette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                this.InitColors();
            }
            base.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.FillRectangle(new SolidBrush(this._gradientMiddleColor), e.ClipRectangle);
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        [Browsable(true), Category("Appearance-Extended"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), DefaultValue("Color.Gray")]
        public Color GradientMiddleColor
        {
            get
            {
                return this._gradientMiddleColor;
            }
            set
            {
                this._gradientMiddleColor = value;
                base.Invalidate();
            }
        }
    }
}

